const TOKENKEY = 'token_key'
const USERIDKEY = 'userId'

// 检查是否在浏览器环境中
const isBrowser = typeof window !== 'undefined' && typeof localStorage !== 'undefined'

function setToken (token) {
  if (!isBrowser) return null
  return localStorage.setItem(TOKENKEY, token)
}

function getToken () {
  if (!isBrowser) return null
  return localStorage.getItem(TOKENKEY)
}

function clearToken () {
  if (!isBrowser) return null
  return localStorage.removeItem(TOKENKEY)
}

function setUserId (userId) {
  if (!isBrowser) return null
  return localStorage.setItem(USERIDKEY, userId)
}

function getUserId () {
  if (!isBrowser) return null
  return localStorage.getItem(USERIDKEY)
}

function clearUserId () {
  if (!isBrowser) return null
  return localStorage.removeItem(USERIDKEY)
}

function clearAllUserData () {
  clearToken()
  clearUserId()
}

export {
  setToken,
  getToken,
  clearToken,
  setUserId,
  getUserId,
  clearUserId,
  clearAllUserData
}